package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type ChannelStatusEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	ChannelType  string                 // 通道类型
	ChannelName  string                 // 通道名称
	Open         bool                   //是否打开
	ChannelParam map[string]interface{} // 配置参数
}

type channelStatusPo struct {
	Id           int64                  `json:"id"`           // 对象id
	CreateTime   int64                  `json:"createTime"`   // 创建时间
	UpdateTime   int64                  `json:"updateTime"`   // 更新时间
	ChannelType  string                 `json:"channelType"`  // 通道类型
	ChannelName  string                 `json:"channelName"`  // 通道名称
	Open         bool                   `json:"open"`         // 通道名称
	ChannelParam map[string]interface{} `json:"channelParam"` // 配置参数
}

func (e *ChannelStatusEntity) Type() string {
	return "ChannelStatusEntity"
}

func (e *ChannelStatusEntity) TableName() string {
	return "tb_channel"
}

func (e *ChannelStatusEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *ChannelStatusEntity) Instance() *ChannelStatusEntity {
	return &ChannelStatusEntity{
		BaseEntity:   new(commEntity.BaseEntity),
		ChannelParam: make(map[string]interface{}),
	}
}

func (e *ChannelStatusEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *ChannelStatusEntity) NewPo() interface{} {
	return &channelStatusPo{
		ChannelParam: make(map[string]interface{}),
	}
}

func (e *ChannelStatusEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *ChannelStatusEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.ChannelType)
	p = append(p, e.ChannelName)
	return p
}

func (e *ChannelStatusEntity) BuildByJson(jsn *string) error {
	var po channelStatusPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *ChannelStatusEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *ChannelStatusEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*channelStatusPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.ChannelParam = po.ChannelParam
	ptr.ChannelType = po.ChannelType
	ptr.ChannelName = po.ChannelName
	ptr.Open = po.Open
}

func (e *ChannelStatusEntity) BuildToPo() interface{} {
	ptr := e
	po := channelStatusPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.ChannelParam = ptr.ChannelParam
	po.ChannelType = ptr.ChannelType
	po.ChannelName = ptr.ChannelName
	po.Open = ptr.Open

	return &po
}
